$(window).resize(function() {  
$('.share-div').css("display","none");
$(".share-div").removeClass("gongxiang");
$(".share-div").removeClass("fengxiang");
var obj=$(".note-share");
obj.each(function(){
	$(this).removeClass("show");
})
});  
$(function(){  
	//默认选择第一条笔记
	$(".dirtype-select").attr("dataid",$(".note-message:first").attr("dirid"));
	$(".dirtype-select").text($(".note-message:first").attr("dirname"));
	$(".note-message:first").addClass("three-border");
	$(".viewnote-tittle").val($(".note-message:first").find(".row-left-content-tittle-tittle").children("span").text());
	$(".note-content").text($(".note-message:first").find(".row-left-content-content").text());
	if($(".note-message:first").attr("notestar")=="1"){
		$(".checkbox-togger").find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");	
	}
	$(".delet-icon-note").attr("dataid",$(".note-message:first").attr("dataid"));
	$(".checkbox-togger").attr("dataid",$(".note-message:first").attr("dataid"));
	$(".share-yes").attr("dataid",$(".note-message:first").attr("dataid"));
	$(".row-left-content").on("hover",".note-message",function(){	
		$(this).css("background","rgba(183, 33, 1, 0.7)");
		$(this).css("color","white");
		$(this).find(".row-left-content-tittle-tittle").css("color","white");
		if($(".box-title").text()!="共享笔记"&&$(".box-title").text()!="公司笔记"){
			$(this).find(".row-left-content-tittle-icon").css("display","block");
		}
	})
	$(".row-left-content").on("mouseleave",".note-message",function(){
		$(this).css("background","white");
		$(this).css("color","#878787");
		$(this).find(".row-left-content-tittle-tittle").css("color","#4a4a4a");
		$(this).find(".row-left-content-tittle-icon").css("display","none");
	})
	//ajax加载分类
	
	$(".select-tittle").click(function(e){
		newselecttype(e);
		$(".select-div").slideToggle();
		$(".select-type-son").css("display","none");
		
	})	
	$(".select-type-tittle").click(function(e){
		newselecttype(e);
		$(".select-type-son").slideToggle();		
		$(".select-type-father").css("display","none");	
	})	
	//展开关闭
	$(".note-open-close").click(function(){
		if($(this).hasClass("ii")){
			$("#one").attr("class","iconfont icon-jian");	
			$(this).removeClass("ii");
		}else{	
			$("#one").attr("class","iconfont icon-jia");
			$(this).addClass("ii");
		}
		$(".row-left-content").slideToggle();
		$(".row-left-top-update-icon").slideToggle();
	})
	//新建笔记本
	$(".newnote-btn").click(function(){
		$(".newnote-icon-value").val("");
		var bool;
		layer.open({
			type : 1,
			skin : 'demo-class',
			skin : 'yourclass',
			offset : '100px',
			title : '新建笔记本',
			area : [ '450px', '308px' ], // 宽高
			content : $(".newnotedir-div"),
			closeBtn : 0,
			btn : [ '创建', '取消' ],
			btnAlign : 'c',
			yes : function(index, layero) {
				var value=$(".newnote-icon-value").val();
				$.ajax({
					  type:"post",
					  url:"/note/newnotedir",
					  cache:false,
					  async:false,
					  data:{"notedirname":value},
					  dataType:"json",
					  success:function(data){
						  bool=data;
					  }				  
				})	
				if(!bool){
					layer.msg('笔记本重名！！！', function(){ 
					});
				}else{
					layer.close(index);
				}				
			},
			btn2 : function(index, layero) {
				layer.close(index);
			}
		});
	})
	//选中样式	
	$(".row-left-content").on("click",".note-message",function(){
		$(".checkbox-togger").attr("dataid",$(this).attr("dataid"));
		$(".share-yes").attr('dataid',$(this).attr("dataid"));
		$(this).siblings().removeClass("three-border"); 
		$(this).addClass("three-border");
		$(".delet-icon-note").attr("dataid",$(this).attr("dataid"));
		if($(this).attr("notestar")=="1"){
			$(".checkbox-togger").find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");	
		}else{
			$(".checkbox-togger").find("i:first").removeClass("icon-collection_fill").addClass("icon-collection");					
		}
		
		$(".dirtype-select").attr("dataid",$(this).attr("dirid"));
		$(".dirtype-select").text($(this).attr("dirname"));
		console.log($(this).find(".row-left-content-content").text());
		$(".viewnote-tittle").val($(this).find(".row-left-content-tittle-tittle").children("span").text());
		$(".note-content").val($(this).find(".row-left-content-content").text());	
	})
	$(".select-div-son").on("hover","li",function(){
		$(this).find(".dir-btn-delete").css("display","inline-block");
		$(this).css("color","black");
	})
	$(".select-div-son").on("mouseleave","li",function(){
		$(this).find(".dir-btn-delete").css("display","none");
		$(this).css("color","#a29d9d");		
	})
	
	$(".select-div").on("click",".dir-btn-delete",function(){
		var dirid=$(this).attr("dirid");	
		 layer.open({ 
			  type: 1,		
			  skin: 'demo-class',
			  skin: 'yourclass',
			  offset: '100px',
			  title :'提示',
			  area: ["300px","200px"], //宽高
			  content: "\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>删除不可恢复,确定删除?</span>\<\/div>",
			  closeBtn: 0,
			  btn:['确定','关闭'],
			  btnAlign: 'c', 
			  yes:function(index, layero){
				  $.ajax({
					  type:"post",
					  url:"/note/deletenoteid",
					  cache:false,
					  async:false,
					  data:{"dirid":dirid},		
					  success:function(data){}				
				})
				layer.close(index);	
			  },
			  btn2: function(index, layero){
				layer.close(index);		   
			  }
			})	
	})
	//删除笔记	
	$(".row-left-content").on("click",".note-delete",function(){	
		  var noteid=$(this).attr("dataid");
		  var dirid=$(".box-header").children("h3").attr("dataid");
		  if(dirid==undefined){
			  dirid="undefined";
		  }
		  layer.open({ 
			  type: 1,		
			  skin: 'demo-class',
			  skin: 'yourclass',
			  offset: '100px',
			  title :'提示',
			  area: ["300px","200px"], //宽高
			  content: "\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>删除不可恢复,确定删除?</span>\<\/div>",
			  closeBtn: 0,
			  btn:['确定','关闭'],
			  btnAlign: 'c', 
			  yes:function(index, layero){			 
					$(".row-left-content").load("/note/deletenewnote",{"dirid":dirid,"noteid":noteid});
					layer.close(index);						
					setTimeout(function(){ 
						$(".dirtype-select").attr("dataid",$(".note-message:first").attr("dirid"))
						$(".dirtype-select").text($(".note-message:first").attr("dirname"));
						$(".note-message:first").addClass("three-border");
						$(".viewnote-tittle").val($(".note-message:first").find(".row-left-content-tittle-tittle").children("span").text());
						$(".note-content").text($(".note-message:first").find(".row-left-content-content").text());
						if($(".note-message:first").attr("notestar")=="1"){
							$(".checkbox-togger").find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");	
						}
						$(".delet-icon-note").attr("dataid",$(".note-message:first").attr("dataid"));
						$(".checkbox-togger").attr("dataid",$(".note-message:first").attr("dataid"));
						$(".share-yes").attr("dataid",$(".note-message:first").attr("dataid"));
					},500);				  		  
			  },
			  btn2: function(index, layero){
				layer.close(index);		   
			  }
			})	
	})
	//删除
	$('.delet-icon-note').click(function(){
		if($(".box-title").text()=="共享笔记"||$(".box-title").text()=="公司笔记"){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>不能操作此笔记！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}else{
			$(".three-border").find(".note-delete").trigger("click");
		}
	})
	
	$(".select-div-father").click(function(){
		var type;
		if($(this).children("span").text()=="全部笔记"){						
			type="all";
		}else if($(this).children("span").text()=="共享笔记"){
			type="share";
		}else{	
			type="company";
		}
		$(".row-left-content").load("/note/selectdirtype",{"dirid":type});
		$(".box-header").children("h3").text($(this).children("span").text());	
	})
	
	//把笔记换到另外的笔记本
	$(".select-div-type").on("click","li",function(){
		if($(".box-title").text()=="共享笔记"||$(".box-title").text()=="公司笔记"){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>不能操作此笔记！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}else{
			var obj=$(this).children(".dir-btn-type")
			var name=obj.find('span').html();
			var dirid=obj.attr("dirid");
			$(".dirtype-select").attr('dataid',dirid);
			$(".dirtype-select").text(name);	
			var noteid=$(".three-border").attr("dataid");
			$.ajax({
				  type:"post",
				  url:"/note/editnotetype",
				  cache:false,
				  async:false,
				  data:{"dirid":dirid,"noteid":noteid},		
				  datatype:'json',
				  success:function(data){
					  layer.msg('移动笔记本成功',{shift: -1,time:1000}, function(){
						});
				  }			
			})
		}
		
	})
	//失焦触发事件
	$(".viewnote-tittle").blur(function(){
		noteedit();
	})
	$(".note-content").blur(function(){
		noteedit();
	})
	//收藏笔记
	$(".row-left-content").on("click",".note-collection-icon",function(){
		var noteid=$(this).attr("dataid");
		
		if($(this).find("i:first").hasClass("icon-collection_fill")){
			$(this).parent().parent().parent().attr("notestar","0")
			$(this).find("i:first").removeClass("icon-collection_fill").addClass("icon-collection");
			startornot(noteid,"取消");
		}else{
			$(this).find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");					
			$(this).parent().parent().parent().attr("notestar","1")
			startornot(noteid,"收藏");
		}
	})
	//收藏笔记
	$(".checkbox-togger").click(function(){
		var icon=$(".note-collection-icon")
		var id=$(this).attr("dataid");
		if($(".box-title").text()=="共享笔记"||$(".box-title").text()=="公司笔记"){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>不能操作此笔记！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}else{
			if($(this).find("i:first").hasClass("icon-collection_fill")){
				$(this).find("i:first").removeClass("icon-collection_fill").addClass("icon-collection");			
				icon.each(function(){
					if($(this).attr("dataid")==id){
						$(this).find("i:first").removeClass("icon-collection_fill").addClass("icon-collection");
					}
				})
				startornot(id,"取消");
			}else{
				icon.each(function(){
					if($(this).attr("dataid")==id){
						$(this).find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");
					}
				})
				$(this).find("i:first").removeClass("icon-collection").addClass("icon-collection_fill");
				startornot(id,"收藏");
			}
		}
	})
	
	$(".select-div-son").on("click","li",function(){		
		//$(".dirtype-select").text($(this).find(".dir-btn-type").children("span").text());
		$(".dirtype-select").attr("dataid",$(this).find(".dir-btn-type").attr("dirid"));
	})
	//完成笔记
	$(".note-finish").click(function(){
		var tittle=$(".viewnote-tittle").val();
		var content= CKEDITOR.instances.TextArea1.getData();
		var dirid=$(".dirtype-select").attr("dataid");
		if(tittle==""){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>笔记不能为空！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}
		$.ajax({
			  type:"post",
			  url:"/note/newnotefinish",
			  cache:false,
			  async:false,
			  data:{"tittle":tittle,"content":content,"dirid":dirid},		
			  success:function(data){}				  
		})
		 layer.msg('保存成功',{shift: -1,time:1000}, function(){
				refreshPanel(getUrlPrefix("/note"), "笔记管理");
			});	
	})
	$(".mail-btn-cancel").click(function(){		
		var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>正在编辑笔记，确定要退出？</span>\<\/div>";	 
		promptbox(obj);	
	})	
	//选择不同的分类
		$(".select-div-son-son").on("click",".dir-btn-type",function(){
			var dirid=$(this).attr("dirid");
			$(".row-left-content").load("/note/selectdirtype",{"dirid":dirid});
			var type=$(this).children("span").text();
			$(".box-header").children("h3").text(type);
			$(".box-header").children("h3").attr("dataid",dirid);
		})
	//收藏邮件
	$(".type-note-star").click(function(){
		 $(".row-left-content").load("/note/allnotestar");
		 $(".box-header").find("h3").attr("dataid","star");
		 $(".box-header").children("h3").text($(this).attr("type"));
	})
	
	//获取div位置
	$(".viewnote-share-div").click(function(){		
		var obj=$(".share-user-div");		
		obj.each(function(){
			$(this).removeClass("select");
		})		
		$(".share-user-div-top").html("");
		$(".share-value").val("");
		sharedivajax("");
		$(".share-div").removeClass("fengxiang");
		if($(".share-div").hasClass("gongxiang")){
			$(".share-div").slideUp();
			$(".share-div").removeClass("gongxiang");
		}else{
			$(".share-div").slideDown();
			$(".share-div").addClass("gongxiang");
		}
		$(".share-div").css('left',$(this).position().left-308+"px");
		$(".share-div").css('top',$(this).position().top+31+"px");
	})
	
	//分享笔记
	$(".row-left-content").on("click",".note-share",function(){
		$(".share-div").removeClass("gongxiang");
		var y=$(".row-left").height()-$(".share-div").height();
		$(".share-user-div-top").html("");
		$(".share-value").val("");
		sharedivajax("");
		$(".share-div").css('left',-($(".row-left").position().left+$(".row-left").width())+"px");
		$(".share-div").css('top',y+"px");		
		if($(".share-div").hasClass("fengxiang")&&$(this).hasClass("show")){
			$(".share-div").css('display',"none");
			$(".share-div").removeClass("fengxiang");
			var obj=$(".note-share");
			obj.each(function(){
				$(this).removeClass("show");
			})
		}else{
			$(".share-div").css('display',"block");
			$(".share-div").addClass("fengxiang");
			$(this).addClass("show")
		}
	})
	
	//查找分享人
	$('.share-value').bind('input propertychange', function(){
		var value=$(this).val()
		$(".share-user-div-top").html("");
		sharedivajax(value);	
	})
	//取消关闭分享
	$(".share-cance").click(function(){
		$('.share-div').css("display","none");
		$(".share-div").removeClass("gongxiang");
		$(".share-div").removeClass("fengxiang");
		var obj=$(".note-share");
		obj.each(function(){
			$(this).removeClass("show");
		})
	})
	//确定分享
	$(".share-yes").click(function(){	
		var company="undefined";
		var user=[];
		var obj=$('.select');
		if($(".box-title").text()=="共享笔记"||$(".box-title").text()=="公司笔记"){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>不能分享此笔记！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}
		obj.each(function(){
			if($(this).attr("userid")=="all"){
				company=$(this).attr("userid");
			}else{
				user.push($(this).attr("userid"));
			}			
		})
		if(user==""&&company=="undefined"){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>请选择分享人！！！</span>\<\/div>";	 
			promptbox(obj);
			return;
		}
		var noteid=$(".share-yes").attr("dataid");
		$.ajax({
			type:"post",
			url:"/note/sharenote",
			data:{"user":user,"noteid":noteid,"company":company},
			cache:false,
			async:false,
			traditional: true,
			success:function(data){				
			}
		})
		$('.share-div').css("display","none");
		$(".share-div").removeClass("gongxiang");
		$(".share-div").removeClass("fengxiang");
		var obj=$(".note-share");
		obj.each(function(){
			$(this).removeClass("show");
		})
	})
	
	$(".share-div").on("click",".share-user-div",function(){
		if($(this).hasClass("select")){
			$(this).removeClass('select');
		}else{
			$(this).addClass('select');
		}
	})
})
//提示框
function promptbox(content){
	  layer.open({ 
		  type: 1,		
		  skin: 'demo-class',
		  skin: 'yourclass',
		  offset: '100px',
		  title :'提示',
		  area: ["300px","200px"], //宽高
		  content: content,
		  closeBtn: 0,
		  btn:['确定','取消'],
		  btnAlign: 'c', 
		  yes: function(index, layero){
			refreshPanel(getUrlPrefix("/note"), "笔记管理"); 
			layer.close(index);
			},
		  btn2: function(index, layero){
			layer.close(index);		   
		  }
		})
}
function newselecttype(e){
	$(".select-div-son").html("");
	
	$.ajax({
		  type:"post",
		  url:"/note/loadtype",
		  cache:false,
		  async:false,
		  dataType:"json",
		  success:function(data){
			$.each(data,function(i,item){
			var li=$("<li style=' padding: 10px 0px 7px 12px; color: #a29d9d'></li>");			
			var a1=$("<a class='dir-btn-type' dirid='"+item.id+"''><i class='iconfont icon-circle1'></i><span> "+item.name +"</span></a>");
			var a2=$("<a class='dir-btn-delete' dirid='"+item.id+"'' style='margin-left: 28px;display: none;color:#a29d9d'><i class='iconfont icon-trash'></i></a>");
			li.append(a1);
			li.append(a2);
			$(".select-div-son").append(li);
			})
		  }				  
	})		
				
	$(document).one("click",function(){
		$(".select-div").css("display","none");								
	});
	e.stopPropagation();
}
function noteedit(){
	var dataid=$(".three-border").attr("dataid");
	var tittle=$(".viewnote-tittle").val();
	var content=$(".note-content").val();
	$.ajax({
		  type:"post",
		  url:"/note/notedit",
		  cache:false,
		  async:false,
		  data:{"tittle":tittle,"content":content,"noteid":dataid},		
		  success:function(data){}				  
	})
}
//收藏和取消
function startornot(noteid,type){
	$.ajax({
		  type:"post",
		  url:"/note/updatastar",
		  cache:false,
		  async:false,
		  data:{"noteid":noteid,"type":type},		
		  success:function(data){}				  
	})
}
//
function newsharediv(item){
	 var one=$(" <div class='share-user-div' style='cursor: pointer; margin-bottom:2px;' userid='"+item.id+"'></div>");
	 var two=$("<div style='padding: 8px 0 5px 19px;'></div>")
	 var three=$("<div style='display: inline-block;overflow: hidden; width: 36px; height: 36px; border-radius: 50%; border: 1px solid #b9b1b1;'><img src='/file/image/"+item.photo+"' style='width: 36px;height: 36px'/></div>")
	 var four=$("<div style='display: inline-block; position: absolute; margin-top: 11px; margin-left: 10px;'><div>"+item.realName+"</div></div>")
	 two.append(three);
	 two.append(four);
	 one.append(two);
	 $(".share-user-div-top").append(one);

}
/**
 * 加载分享人
 * @param value
 * @returns
 */
function sharedivajax(value){
	$.ajax({
		  type:"post",
		  url:"/note/sharevaluefind",
		  cache:false,
		  async:false,
		  data:{"value":value},	
		  datatype:"json",
		  success:function(data){
			 $.each(data,function(i,item){
				 newsharediv(item);
			 }) 
		  }				  
	})
}